package com.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.constant.Constants;
import com.jl.commons.result.PageDataInfo;
import com.jl.mapper.DeviceMapper;
import com.jl.mapper.ProductionLineMapper;
import com.jl.model.ProductionLine;
import com.jl.model.in.ProductionLineDevice;
import com.jl.model.in.ProductionLineIn;
import com.jl.service.IProductionLineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 生产线表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Service
public class ProductionLineServiceImpl extends ServiceImpl<ProductionLineMapper, ProductionLine> implements IProductionLineService {

    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void selectProductionLineDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<ProductionLine> qw) {
        Page<Map<String,Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String,Object>> mapList =baseMapper.selectDataGrid(page,qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public List<Map<String, Object>> getLineFaultStatistics(Long lineId, Integer type) {
        List<Map<String, Object>> dataInfo = new ArrayList<>();
        if(Constants.FAULT_COUNT_TOP_TYPE.equals(type)){
            //故障次数TOP设备
            dataInfo = baseMapper.selectTopFiveFaultCountInLine(lineId);
        }else if(Constants.FAULT_OVER_HOUR_COUNT_TOP_TYPE.equals(type)){
            //故障时间超2小时故障次数TOP设备
            dataInfo = baseMapper.selectTopFiveOverHourCountInLine(lineId);
        }else if(Constants.FAULT_TOTAL_TIME_TOP_TYPE.equals(type)){
            //故障时间TOP设备
            dataInfo = baseMapper.selectTopFiveTotalTimeInLine(lineId);
        }
        return dataInfo;
    }

    @Override
    public boolean isProductionLineNameUsed(String name) {
        return baseMapper.selectProductionLineNameCount(name)>0;
    }

    @Override
    public boolean isProductionLineNameCanUse(String name,Long productionLineId) {
        ProductionLineIn productionLineIn = baseMapper.selectProductionLineInByName(name);
        if(productionLineIn == null){
            //如果找不到数据则表示该数据没有被使用
            return true;
        }else{
            if(productionLineIn.getId().equals(productionLineId)){
                //如果这个数据就是他本身则可以使用
                return true;
            }
        }
        return false;
    }
}
